package com.example.demo.api;

import com.example.demo.model.Friend;
import com.example.demo.model.FriendMapper;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendAPI {
    @Autowired
    private FriendMapper friendMapper;

    @RequestMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest request) {
        // 先从会话中获取userID
        HttpSession session = request.getSession(false);
        if(session == null) {
            // 未登录
            // 返回空的列表即可
            System.out.println("[getFriendList] session 不存在");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            // 当前用户不存在
            System.out.println("[getFriendList] user 不存在");
            return new ArrayList<Friend>();
        }
        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return friendList;
    }
}
